package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.javaBean.Activity;
import com.yuejian.neusoft.yuejian_24.javaBean.User;
import com.yuejian.neusoft.yuejian_24.listener.ActivityListener;
import com.yuejian.neusoft.yuejian_24.listener.UserListener;
import com.yuejian.neusoft.yuejian_24.model.ActivityModel;
import com.yuejian.neusoft.yuejian_24.model.UserModel;

public class ActivityJoinActivity extends AppCompatActivity {

    private TextView ac_name,ac_price,username,mustknow,count,buy;
    private ImageView jia,jian,back;
    private EditText userphone,num;
    private Intent intent;
    private SharedPreferences sp;

    ActivityListener activityListener = new ActivityListener() {
        @Override
        public void onResponse(Activity info) {
            ac_name.setText(info.getAct_name());
            ac_price.setText(info.getMoney()+"￥/人");
            mustknow.setText("\u3000\u3000"+info.getAct_content());
            double abc=Double.parseDouble(info.getMoney().toString());
            count.setText("合计："+String.valueOf(abc)+"￥");
        }

        @Override
        public void onFail() {

        }
    };

    UserListener userListener = new UserListener() {
        @Override
        public void onResponse(User info) {
            username.setText(info.getUsername());
            userphone.setText(info.getUser_phone());
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_join);
        initViews();
        ActivityModel activityModel= new ActivityModel();
        activityModel.getacdetail(getIntent().getStringExtra("act_id").toString(),activityListener);
        UserModel userModel = new UserModel();
        userModel.getuserinfo(sp.getString("uid",""),userListener);
        initEvent();
        count();
    }

    private void count() {
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                double sum = Double.parseDouble(ac_price.getText().toString().split("￥")[0]) *
                        Double.parseDouble(num.getText().toString());
                count.setText("合计："+String.valueOf(sum)+"￥");
            }
        });
    }

    private void initEvent() {
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(num.getText().toString())+1;
                num.setText(String.valueOf(number));
            }
        });

        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(num.getText().toString())-1;
                num.setText(String.valueOf(number));
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ActivityJoinActivity.this,"报名成功！", Toast.LENGTH_SHORT).show();
                finish();

            }
        });
    }

    private void initViews() {
        ac_name = (TextView)findViewById(R.id.activity_join_name);
        ac_price = (TextView)findViewById(R.id.activity_join_price);
        username = (TextView)findViewById(R.id.activity_join_username);
        userphone = (EditText) findViewById(R.id.activity_join__phone);
        mustknow = (TextView)findViewById(R.id.activity_join_know);
        buy = (TextView)findViewById(R.id.aactivity_join_buy);
        count = (TextView)findViewById(R.id.activity_join_count);
        jia =(ImageView)findViewById(R.id.activity_join_jia);
        jian = (ImageView)findViewById(R.id.activity_join_jian);
        num = (EditText)findViewById(R.id.activity_join_num);
        back = (ImageView)findViewById(R.id.activity_join_back);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }
}
